const ResponseStatusCode = {
    ok: {
       code: 200,
       msg: '成功'
    },
    err: {
       code: 500,
       msg: '失败'
    },
    warning: {
       code: 600,
       msg: '警告'
    }
};

const ResponseStatusMsg  = {
  ok: "success",
  err: 500,
  warning: 501
};


module.exports.setLocalsDefault = (req, res, next) => {
  res.locals.code = 200;
  res.locals.err = "";
  res.locals.data = {};
  next();
};

module.exports.setLocalsState = function (res, state) {
  Object.entries(state).forEach(([key, value]) => {
    res.locals.data[key] = value;
  });
};

module.exports.sendResult = function (res, data, status = "ok") {
  const resStatus = ResponseStatusCode[status];
  res.send({
     data,
     ...resStatus
  })
};

const packageDesign = function (data, status = "ok") {
  const resStatus = ResponseStatusCode[status];
  return {
     data,
     ...resStatus
  }
};

module.exports.packageDesign = packageDesign;


module.exports.sendResult = function (res, data, status = "ok") {
  const result = packageDesign(data, status);
  res.send(result);
};
